package in.abip.chisame;

import java.util.List;

import in.abip.chisame.model.Camera;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;

/**
 * Camera list activity.
 * 
 * @author Julot.
 * @since Thursday, January 26th, 2011.
 */
public class CameraListActivity extends ChisameActivity {
	private ListAdapter _adapter;

	/** Called when the activity is first created. */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list_activity);
		bind();
	}

	/**
	 * We need to repopulate after camera addition, thus it's here. The better
	 * way is using startActivityForResult and override onActivityResult in main
	 * activity, but it's too complex for now, maybe later.
	 */
	@Override
	protected void onResume() {
		super.onResume();
		populate();
	}

	/** Populate room list by item from database. */
	@Override
	protected void populate() {
		ListView list = (ListView) findViewById(R.id.list);
		_adapter = new ListAdapter(this, R.layout.list_item, loadModels());
		list.setAdapter(_adapter);
	}

	/** Load models */
	private List<Object> loadModels() {
		Bundle bundle = getIntent().getExtras();
		List<Object> models = null;
		if (bundle != null) {
			models = Camera.model().findAll(
				"roomId = " + bundle.getInt("roomId"));
		} else
			models = Camera.model().findAll();
		return models;
	}

	/** Bind camera list view to listener. */
	private void bind() {
		OnItemClickListener listener = new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View itemClicked,
				int position, long id) {
				ListAdapter adapter;
				adapter = (ListAdapter) parent.getAdapter();
				Camera camera = (Camera) adapter.getItem(position);
				Bundle bundle = new Bundle();
				bundle.putInt("cameraId", camera._id);
				Intent intent = new Intent(CameraListActivity.this,
					MjpegActivity.class);
				intent.putExtras(bundle);
				startActivity(intent);
			}
		};
		ListView list = (ListView) findViewById(R.id.list);
		list.setOnItemClickListener(listener);
		registerForContextMenu(list);
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
		ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		menu.setHeaderTitle("Camera");
		menu.add(0, v.getId(), 0, R.string.edit);
		menu.add(0, v.getId(), 0, R.string.delete);
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		AdapterContextMenuInfo info;
		info = (AdapterContextMenuInfo) item.getMenuInfo();
		Camera camera = (Camera) _adapter.getItem(info.position);
		if (item.getTitle().equals(getString(R.string.delete))) {
			camera.delete();
			populate();
		} else {
			Bundle bundle = new Bundle();
			bundle.putInt("cameraId", camera._id);
			Intent intent = new Intent(CameraListActivity.this,
				CameraFormActivity.class);
			intent.putExtras(bundle);
			startActivity(intent);
			
		}
		return true;
	}

}
